package cn.tkb.org.controller;
import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.Log;
import cn.tkb.org.domain.Department;
import cn.tkb.org.query.DepartmentQuery;
import cn.tkb.org.service.IDepartmentService;
import cn.tkb.basic.util.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/dept")

public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //新增和修改
    @RequestMapping
    @Log("新增或者修改")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    //查询部门
    @Log("查询")
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.QeuryById(id);
    }
    //删除部门
    @Log("删除")
    @DeleteMapping("/{id}")
        public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }

    }
    //批量删除
    @Log("批量删除")
    @PatchMapping
    public AjaxResult BatchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.BatchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail(e.getMessage());
        }
    }
    //查询所有部门
    @Log("查询所有")
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.QeuryAll();
    }
    //高级查询和分页查询
    @Log("高级查询")
    @PostMapping
    public PageQuery<Department> queryPage(@RequestBody DepartmentQuery query){
       return departmentService.findPage(query);
    }
    //查询所有部门
    @Log("查询所有部门树")
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.QeuryTree();
    }
}
